﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using DrawingScreen.Constant;
using System.Xml;

namespace DrawingScreen
{
    public class Global
    {
        public static int SCREEN_WIDTH = 1;
        public static int SCREEN_HEIGHT = 1;

        
        
        public static Dictionary<int, int> prev_ClientID_Mode_dictionary = new Dictionary<int, int>();

        public static int GetModeByID(int clientID)
        {
            bool result1 = Global.prev_ClientID_Mode_dictionary.ContainsKey(clientID);

            int mode = CMode.FREEDOM;

            if (result1 == true)
            {
                mode = Global.prev_ClientID_Mode_dictionary[clientID];
            }
            else
            {
                Global.prev_ClientID_Mode_dictionary.Add(clientID, mode);
            }

            return mode;
        }

        public static Dictionary<int, Pen> prev_ClientID_Pen_dictionary = new Dictionary<int, Pen>();

        public static Pen GetPenByID(int clientID)
        {
            Pen CurrentPen = new Pen(Color.Blue, 1);

            bool result = Global.prev_ClientID_Pen_dictionary.ContainsKey(clientID);

            if (result == true)
            {
                CurrentPen = Global.prev_ClientID_Pen_dictionary[clientID];
            }
            else
            {
                Global.prev_ClientID_Pen_dictionary.Add(clientID, CurrentPen);
            }

            return CurrentPen;
        }

        public static Dictionary<int, int> prev_ClientID_PointerID_dictionary = new Dictionary<int, int>();

        public static int GetPointerIDByID(int clientID)
        {
            int id = 0;

            bool result = Global.prev_ClientID_PointerID_dictionary.ContainsKey(clientID);

            if (result == true)
            {
                id = Global.prev_ClientID_PointerID_dictionary[clientID];
            }
            else
            {
                Global.prev_ClientID_PointerID_dictionary.Add(clientID, id);
            }

            return id;
        }

        public static Dictionary<int, int> prev_ClientID_ClipArtID_dictionary = new Dictionary<int, int>();

        public static int GetClipArtIDByID(int clientID)
        {
            int id = 0;

            bool result = Global.prev_ClientID_ClipArtID_dictionary.ContainsKey(clientID);

            if (result == true)
            {
                id = Global.prev_ClientID_ClipArtID_dictionary[clientID];
            }
            else
            {
                Global.prev_ClientID_ClipArtID_dictionary.Add(clientID, id);
            }

            return id;
        }

        public static void InitForNewClient(int newClientId)
        {
            // TO DO: initialize state

            if (!Global.prev_ClientID_Mode_dictionary.ContainsKey(newClientId))
            {
                Global.prev_ClientID_Mode_dictionary.Add(newClientId, CMode.POINTER);
            }

            if (!Global.prev_ClientID_Pen_dictionary.ContainsKey(newClientId))
            {
                Global.prev_ClientID_Pen_dictionary.Add(newClientId, new Pen(Color.Blue, 5));
            }

            if (!Global.prev_ClientID_PointerID_dictionary.ContainsKey(newClientId))
            {
                Global.prev_ClientID_PointerID_dictionary.Add(newClientId, 1);
            }

            if (!Global.prev_ClientID_ClipArtID_dictionary.ContainsKey(newClientId))
            {
                Global.prev_ClientID_ClipArtID_dictionary.Add(newClientId, 0);
            }
        }

        public static XmlElement CreateXmlElementFromPen(XmlDocument doc, Pen pen)
        {
            XmlElement elePen = doc.CreateElement("PEN");

            XmlAttribute attrRed = doc.CreateAttribute("red");
            attrRed.Value = pen.Color.R.ToString();

            XmlAttribute attrGreen = doc.CreateAttribute("green");
            attrGreen.Value = pen.Color.G.ToString();

            XmlAttribute attrBlue = doc.CreateAttribute("blue");
            attrBlue.Value = pen.Color.B.ToString();

            XmlAttribute attrWidth = doc.CreateAttribute("width");
            attrWidth.Value = pen.Width.ToString();

            elePen.Attributes.Append(attrRed);
            elePen.Attributes.Append(attrGreen);
            elePen.Attributes.Append(attrBlue);
            elePen.Attributes.Append(attrWidth);

            return elePen;
        }

        public static Pen CreatePenFromXmlElement(XmlElement elePen)
        {
            try
            {
                int r = Int32.Parse(elePen.Attributes["red"].Value);
                int g = Int32.Parse(elePen.Attributes["green"].Value);
                int b = Int32.Parse(elePen.Attributes["blue"].Value);
                float width = float.Parse(elePen.Attributes["width"].Value);

                return new Pen(Color.FromArgb(r, g, b), width);
            }
            catch
            {
                return null;
            }
        }

        public static XmlElement CreateXmlElementFromRectangle(XmlDocument doc, Rectangle rect)
        {
            XmlElement eleRECT = doc.CreateElement("RECT");

            XmlAttribute attrX = doc.CreateAttribute("x");
            attrX.Value = rect.X.ToString();

            XmlAttribute attrY = doc.CreateAttribute("y");
            attrY.Value = rect.Y.ToString();

            XmlAttribute attrW = doc.CreateAttribute("w");
            attrW.Value = rect.Width.ToString();

            XmlAttribute attrH = doc.CreateAttribute("h");
            attrH.Value = rect.Height.ToString();

            eleRECT.Attributes.Append(attrX);
            eleRECT.Attributes.Append(attrY);
            eleRECT.Attributes.Append(attrW);
            eleRECT.Attributes.Append(attrH);

            return eleRECT;
        }

        public static Rectangle CreateRectangleFormXmlElement(XmlElement eleRect)
        {
          
                int x = Int32.Parse(eleRect.Attributes["x"].Value);
                int y = Int32.Parse(eleRect.Attributes["y"].Value);
                int w = Int32.Parse(eleRect.Attributes["w"].Value);
                int h = Int32.Parse(eleRect.Attributes["h"].Value);

                return new Rectangle(x, y, w, h);

        }

        public static Rectangle CreateRectangle(int i_x, int i_y, int i_w, int i_h)
        {
            int x, y, w, h;

            if (i_w > 0)
            {
                x = i_x;
            }
            else
            {
                x = i_x + i_w;
            }

            if (i_h > 0)
            {
                y = i_y;
            }
            else
            {
                y = i_y + i_h;
            }

            w = System.Math.Abs(i_w);
            h = System.Math.Abs(i_h);

            return new Rectangle(x, y, w, h);
        }
    }
}
